package com.catsic.mod.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.core.UserInfo;
import com.catsic.core.entity.Organ;
import com.catsic.core.organ.service.OrganService;
import com.catsic.mod.condition.WserviceCondition;
import com.catsic.mod.convert.Converts;
import com.catsic.mod.dao.WserviceDao;
import com.catsic.mod.entity.Wservice;
import com.catsic.mod.form.WserviceForm;
import com.catsic.mod.service.WserviceService;
import com.catsic.mod.specification.WserviceSpec;
import com.catsic.mod.status.WserviceStatus;
import com.catsic.pub.drawer.service.DrawerService;
import com.catsic.pub.entity.Drawer;
import com.catsic.pub.form.DrawerForm;
@Service("wserviceService")
public class WserviceServiceImpl extends BaseServiceImpl<Wservice, String>
		implements WserviceService {

	@Autowired
	private WserviceDao wserviceDao;
	
	@Autowired
	private OrganService organService;
	@Autowired
	private DrawerService drawerService;

	@Override
	public WserviceForm add(WserviceForm form){
		Object principal =SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Wservice wservice = new Wservice();
		Organ  organ=null;
		if (principal instanceof UserInfo) { 
		 if (((UserInfo) principal).getCorganId()!=null) {
			 organ = organService.findById(((UserInfo) principal).getCorganId());
		}
		 if (((UserInfo) principal).getPorganId()!=null) {
			organ=organService.findById(((UserInfo) principal).getPorganId());
		}
		}
		
		wservice.setDrawer(drawerService.findById(form.getType()));
	
		wservice.setOrgan(organ);
		wservice.setMethodName(form.getMethodName());
		wservice.setStatus(form.getStatus());
		wservice.setUrl(form.getUrl());
		wservice.setName(form.getName());
		super.add(wservice);
		return form;
	}
	
	@Override
	public WserviceForm findByType(String type){
		
		List<Wservice> list = wserviceDao.findByDrawerId(type);
		if(CollectionUtils.isNotEmpty(list)){
			List<WserviceForm> listform = new ArrayList<WserviceForm>();
			WserviceForm form = new WserviceForm();
			for(Wservice wservice:list){
				form = Converts.convert(wservice);
				listform.add(form);
			}
			return listform.get(0);
		}
		return null;
	}
	
	@Override
	public void report(String id){
		Wservice wservice = findById(id);
		wservice.setStatus(WserviceStatus.已报送);
		super.update(wservice);
	}
	
	@Override
	public WserviceForm update(WserviceForm form){
		Object principal =SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Wservice wservice = findById(form.getId());
		Organ  organ=null;
		if (principal instanceof UserInfo) { 
		 if (((UserInfo) principal).getCorganId()!=null) {
			 organ = organService.findById(((UserInfo) principal).getCorganId());
		}
		 if (((UserInfo) principal).getPorganId()!=null) {
			organ=organService.findById(((UserInfo) principal).getPorganId());
		}
		}
		wservice.setDrawer(drawerService.findById(form.getType()));
		wservice.setOrgan(organ);
		wservice.setMethodName(form.getMethodName());
		wservice.setStatus(form.getStatus());
		wservice.setUrl(form.getUrl());
		wservice.setName(form.getName());
		super.update(wservice);
		return form;
	}
	
	@Override
	public Wservice findById(String id){
		Wservice wservice = getById(id);
	/*	if(null == wservice){
			throw new CatsicException(CatsicExceptionCode.ENTITY_NOT_FOUND,Constants.接口实体);
		}*/
		return wservice;
	}
	
	@Override
	public List<DrawerForm> findByDrawerNotWservice(String organId,String drawerId){
		List<DrawerForm> formList = new ArrayList<DrawerForm>();
		List<Drawer> list = wserviceDao.findByDrawerNotWservice(organId,drawerId);
		for(Drawer drawer : list){
			formList.add(com.catsic.pub.convert.Converts.convert(drawer));
		}		
		return formList;
	}


	@Override
	public WserviceForm findByWserviceId(String id) {
		Wservice wservice = findById(id);
		return Converts.convert(wservice);
	}

	@Override
	public Page<WserviceForm> findAll(WserviceCondition condition,
			Pageable pageable) {
		WserviceSpec spec = new WserviceSpec(condition);
		Page<Wservice> page = wserviceDao.findAll(spec,pageable);
		WserviceForm form = new WserviceForm();
		List<WserviceForm> listForm = new ArrayList<WserviceForm>();
		for(Wservice wservice : page.getContent()){
			form = Converts.convert(wservice);
			listForm.add(form);
		}
		Page<WserviceForm> page_ = new PageImpl<WserviceForm>(listForm,pageable,page.getTotalElements());
		return page_;
	}

	@Override
	public List<WserviceForm> findByDrawerId(String id) {
		List<Wservice>  wservices=wserviceDao.findByDrawerId(id);
		List<WserviceForm>  list=new ArrayList<WserviceForm>();
		for(int i=0;i<wservices.size();i++ ){
			list.add(Converts.convert(wservices.get(i)));
			
		}
		return list;
	}
	

}
